﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Class;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Drawing;




namespace WebApplication2
{
    public partial class About : Page
    {
        int language = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl currdiv = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("menu");
            currdiv.Visible = false;
            System.Web.UI.HtmlControls.HtmlGenericControl button = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("button");
            button.Visible = false;
            language = (int)Session["Language"];
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("hi-IN");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi-IN");
            Deserialize_json_Object_and_Bind_GridView();
        }

        public void Deserialize_json_Object_and_Bind_GridView()
        {
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/GetTodayPatient");
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"GetTodayPatient\":" + json + "}";
          
            JavaScriptSerializer ser = new JavaScriptSerializer();
            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapper>(json);
            GridView1.DataSource = keysWrapper.gettodaypatient;
            GridView1.DataBind();
            test();
        }

        public void test()
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string type = GridView1.Rows[i].Cells[10].Text;
                
                if (type == "1")
                {
                    GridView1.Rows[i].Cells[9].BackColor = System.Drawing.Color.Gray;
                }
                if (type == "2")
                {
                    GridView1.Rows[i].Cells[9].BackColor = System.Drawing.Color.Green;
                }
                if (type == "3")
                {
                    GridView1.Rows[i].Cells[9].BackColor = System.Drawing.Color.Cyan;
                }
                if (type == "4")
                {
                    GridView1.Rows[i].Cells[9].BackColor = System.Drawing.Color.Yellow;
                }

                bool isNoData = Convert.ToBoolean(GridView1.Rows[i].Cells[11].Text);
                if (isNoData == false)
                {
                    GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
                }
                
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
                e.Row.ToolTip = "Click to select this row.";
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (e.Row.Cells.Count > 0)
                {
                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXTodayLabel?LanguageID=" + language);
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"FMTXTodayLabel\":" + json + "}";
                    
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var KeyWapper= Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWapperTodayLabel>(json);

                    foreach (var item in KeyWapper.fmtxtodaylabel)
                    {
                        if (item.FormLabelCode == "Today-Name")
                        {
                            e.Row.Cells[0].Text = item.Language.ToString();
                            e.Row.Cells[0].Width = 150;
                        }

                        if (item.FormLabelCode == "Today-MRN")
                        {
                            e.Row.Cells[1].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-ID")
                        {
                            e.Row.Cells[2].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-Sex")
                        {
                            e.Row.Cells[3].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-Stn")
                        {
                            e.Row.Cells[4].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-STime")
                        {
                            e.Row.Cells[5].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-DW")
                        {
                            e.Row.Cells[6].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-PreWt")
                        {
                            e.Row.Cells[7].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-PostWt")
                        {
                            e.Row.Cells[8].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Today-Status")
                        {
                            e.Row.Cells[9].Text = item.Language.ToString();
                            e.Row.Cells[9].Width = 100;
                        }
                        e.Row.Cells[10].Visible = false;
                        e.Row.Cells[11].Visible = false;
                    }
                }
            }
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].Cells[10].Visible = false;
                GridView1.Rows[i].Cells[11].Visible = false;
            }
        }

        protected void lnkView_Click(object sender, EventArgs e)
        {
            GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
            string PatientName = grdrow.Cells[0].Text;
            string Mrn = grdrow.Cells[1].Text;
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/GetTodayPatient");
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"GetTodayPatient\":" + json + "}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapper>(json);

            foreach (var item in keysWrapper.gettodaypatient)
            {
                if (item.MRN == Mrn)
                {
                    DataForFlowChart g = new DataForFlowChart();
                    g.pName = PatientName;
                    g.Date_Time1 = DateTime.Now.ToString("yyyy/MM/dd");

                    g.HDTreatmentID = Convert.ToInt32(item.HDTreatmentID);
                    g.PatientID = Convert.ToInt32(item.PatientID);
                    g.RunNo = Convert.ToInt32(item.Run_No);
                    g.PID = item.MRN;
                    g.mrn = item.MRN;
                    Session["DataForFlowChart"] = g;
                    Response.Redirect("FlowChart.aspx?");
                }
            }
        }

        protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    string mrn = row.Cells[1].Text;
                    
                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/GetTodayPatient");
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"GetTodayPatient\":" + json + "}";

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapper>(json);

                    foreach (var item in keysWrapper.gettodaypatient)
                    {
                        if (item.MRN == mrn)
                        {
                            DataForSummaryScreen DFSS = new DataForSummaryScreen();
                            DFSS.Arrival_Time = item.Arrival_Time;
                            DFSS.PatientID = Convert.ToInt32(item.PatientID);
                            Session["DataForSummaryScreen"] = DFSS;
                            int treatmentID = Convert.ToInt32(item.HDTreatmentID);
                            Session["treatmentID"] = treatmentID;
                        }
                    }

                    Session["Language"] = language;
                    Response.Redirect("Summary.aspx?");
                }
                else
                {
                    row.ToolTip = "Click to select this row.";
                }
            }
        }

    
    }    
}
